package com.ysxsoft.wdg.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.home.SearchHisAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BannerBean;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class JDSearchActivity extends BaseActivity {


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    EditText etContent;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private SearchHisAdapter mAdapter;
    private String uid = "";

    public static void start(Context context){
        Intent intent=new Intent(context,JDSearchActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tb_search;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);

        uid = SharePrefUtils.getUserId();

        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new SearchHisAdapter(R.layout.item_his_text);
        recyclerview.setAdapter(mAdapter);
        getHis();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getHis();
    }

    private void getHis() {
        mApiHelper.getHisSearch(uid, "5", new Observer<BaseBean<List<BannerBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<BannerBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    mAdapter.setNewData(baseBean.getData());
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void setListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BannerBean bean= (BannerBean) adapter.getItem(position);
                JDSearchListActivity.start(mContext,bean.getKeyword());
            }
        });
    }

    @OnClick({R.id.title_finish, R.id.title_tv_r, R.id.iv_del})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.title_tv_r:
                String content = etContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.showToast("请输入搜索的商品");
                    return;
                }
                JDSearchListActivity.start(mContext,content);
                break;
            case R.id.iv_del:
                mApiHelper.clearHisSearch(uid, "5", new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        ToastUtils.showToast(baseBean.getMs());
                        if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                            mAdapter.clear();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showToast("请稍后再试");
                    }

                    @Override
                    public void onComplete() {
                    }
                });
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        ViewUtils.closeKeyboard(mContext);
    }
}
